{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Annotation](07.09-Annotation.ipynb) | [Contents](Index.ipynb) | [Shared Axis](07.11-Shared-Axis.ipynb)>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.10 底图\n",
    "\n",
    "`mpl_toolkits.basemap`中的`basemap`库提供了在地球上显示一些变量的选项，在最常用的投影中显示边界(水文、政治等)。我们将使用Berambadi流域边界绘制band1数据。图7.3显示了带有白色标记的流域边界的band1数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named basemap",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-c2121015aa01>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mmpl_toolkits\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbasemap\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mBasemap\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mgdal\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mgdalconst\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: No module named basemap"
     ]
    }
   ],
   "source": [
    "from mpl_toolkits.basemap import Basemap\n",
    "import gdal\n",
    "from gdalconst import *\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "# read the data\n",
    "dataset = gdal.Open(\"datas/band1.tif\",GA_ReadOnly)\n",
    "band1 = dataset.GetRasterBand(1).ReadAsArray()\n",
    "GT = dataset.GetGeoTransform()\n",
    "\n",
    "dataset = None\n",
    "\n",
    "# make the co ordinate for the berambadi\n",
    "lon = np.linspace(GT[0]+GT[1]/2, GT[0]+GT[1]*(band1.shape[1]-0.5), band1.shape[1])\n",
    "lat = np.linspace(GT[3]+GT[5]/2, GT[3]+GT[5]*(band1.shape[0]-0.5), band1.shape[0])\n",
    "Lon, Lat = np.meshgrid(lon, lat)\n",
    "\n",
    "# make the base map\n",
    "m = Basemap(projection='merc',llcrnrlat=11.72,urcrnrlat=11.825,\\\n",
    "          llcrnrlon=76.51,urcrnrlon=76.67,lat_ts=20,resolution=None)\n",
    "\n",
    "# draw parallels and meridians.\n",
    "m.drawparallels(np.arange(11.7,11.9,.05),labels=[1,0,0,0])\n",
    "m.drawmeridians(np.arange(76.4,76.8,.05),labels=[0,0,0,1])\n",
    "\n",
    "# read the shapefile archive\n",
    "s = m.readshapefile('datas/berambadi','berambadi',color='b',linewidth=2.5)\n",
    "\n",
    "# compute native map projection coordinates of lat/lon grid.\n",
    "x, y = m(Lon,Lat)\n",
    "\n",
    "# contour data over the map\n",
    "cs = m.pcolor(x,y,band1,cmap=plt.cm.jet)\n",
    "cb = plt.colorbar(cs, shrink=0.6, extend='both')\n",
    "\n",
    "plt.title(\"Band1\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
